package main.mvc;

import static main.mvc.utils.BuildHelper.buildEntityName;
import static main.mvc.utils.BuildHelper.getImport;
import static main.mvc.utils.BuildHelper.getNAndT;
import static main.mvc.utils.BuildHelper.getPropertiesparse;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;

public class ServiceIntefaceBuilder {
	
	public static void buildService(String tableName){
		String entityName = buildEntityName(tableName, true);
		String subName = buildEntityName(tableName, false);
		String subNameDao = subName + "Dao";
		String entityNameForm = entityName + "Form";
		String subNameForm = subName + "Form";
		String _java = buildHead(entityName) + buildServiceStr(entityName) + 
			buildInsertStr(entityName, subName, entityNameForm, subNameForm, subNameDao) + 
			buildUpdateStr(entityName, subName, entityNameForm, subNameForm, subNameDao) + 
			buildDeleteStr(subNameDao) + buildGetByIdStr(entityName, subNameDao) + 
			buildGetListStr(entityName, subNameDao) + buildGetPageStr(entityName, subNameDao) + "\n}";
//		System.out.println(_java);
		
		String folderName = getPropertiesparse().getValue("service.build.path");
		String fileName = getPropertiesparse().getValue("service.build.path") + File.separator + entityName + "Service.java";
		String isUpdate = getPropertiesparse().getValue("service.build.rebuild");
		try {
			File folder = new File(folderName);
			if(!folder.exists()){
				folder.mkdirs();
			}
			File file = new File(fileName);
			if(!file.exists()){
				file.createNewFile();
				BufferedWriter out = new BufferedWriter(new FileWriter(file));
				out.write(_java);
				out.flush();
				out.close();
			} else {
				if(isUpdate.equals("true")){
					file.delete();
					BufferedWriter out = new BufferedWriter(new FileWriter(file));
					out.write(_java);
					out.flush();
					out.close();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/** head */
	public static String buildHead(String entityName){
		StringBuffer head = new StringBuffer();
		head.append("package " + getPropertiesparse().getValue("service.build.package") + ";");
		head.append(getNAndT(2, 0) + getImport(1));
		
		head.append(getNAndT(2, 0) + getImport(7) + entityName + "Form;");
		head.append(getNAndT(1, 0) + getImport(6) + entityName +";");
		head.append(getNAndT(1, 0) + getImport(15));
		head.append(getNAndT(1, 0) + getImport(16));
		head.append(getNAndT(1, 0) + getImport(3));
		
		return head.toString();
	}
	
	/** public interface XXXService {  */
	public static String buildServiceStr(String entityName) {
		return getNAndT(2, 0) + "public interface " + entityName + "Service {";
	}
	
	/** public boolean insert(Entity entity); */
	public static String buildInsertStr(String entityName, String subName, String entityNameForm, String subNameForm, String subNameDao) {
		return getNAndT(2, 1) + "public boolean insert(" + entityNameForm + " "	+ subNameForm + ") throws ServiceException;";
	}

	/** public boolean update(Entity entity); */
	public static String buildUpdateStr(String entityName, String subName, String entityNameForm, String subNameForm, String subNameDao) {
		return getNAndT(2, 1) + "public boolean update(" + entityNameForm + " " + subNameForm + ") throws ServiceException;";
	}
	
	/** public boolean detele(Integer id); */
	public static String buildDeleteStr(String subNameDao) {
		return getNAndT(2, 1) + "public boolean delete(Integer id) throws ServiceException;";
	}
	
	/** public Entity getById(Integer id); */
	public static String buildGetByIdStr(String entityName, String subNameDao) {
		return getNAndT(2, 1) + "public " + entityName + " getById(Integer id) throws ServiceException;";
	}
	
	/** public List<Entity> queryForList(); */
	public static String buildGetListStr(String entityName, String subNameDao) {
		return getNAndT(2, 1) + "public List<" + entityName + "> queryForList() throws ServiceException;";
	}
	
	/** public PageView<Entity> queryForPage(Page page); */
	public static String buildGetPageStr(String entityName, String subNameDao) {
		return getNAndT(2, 1) + "public PageView<" + entityName + "> queryForPage(Page page) throws ServiceException;";
	}
	
	public static void main(String args[]){
		buildService("area_collages");
	}
	
}
